package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.License;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Arrays.stream;

@Service
public class  ReportServiceImpl implements ReportService {

    @Autowired
    OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {

        ///获取日期列表
        List<LocalDate> dateList = new ArrayList<>();
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //获取营业额
        List<Double> list = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap<>();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            map.put("status", Orders.COMPLETED);
            Double mount = orderMapper.getAllmountBydate(map);
            //空值处理
            mount = mount == null ? 0.0: mount;
            list.add(mount);

        }
         return TurnoverReportVO.
                 builder()
                 .dateList(StringUtils.join(dateList,","))
                 .turnoverList(StringUtils.join(list,","))
                 .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {

        ///获取日期列表
        List<LocalDate> dateList = new ArrayList<>();
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //获取用户数
        List<Integer> totalUserList = new ArrayList<>();//总用户数
        List<Integer> newUserList = new ArrayList<>();//新增用户数

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();
            map.put("endTime",endTime);
            Integer totaluser = orderMapper.getUser(map);
            totaluser = totaluser == null ? 0 : totaluser;
            totalUserList.add(totaluser);

            map.put("beginTime",beginTime);
            Integer newuser = orderMapper.getUser(map);
            newuser = newuser == null ? 0 : newuser;
            newUserList.add(newuser);
        }

        return UserReportVO.builder()
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .dateList(StringUtils.join(dateList,","))
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {

        //获取日期列表
        List<LocalDate> dateList = new ArrayList<>();

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //获取每日订单数
        List<Integer> orderCountList =  new ArrayList<>();
        //获取每日有效订单数
        List<Integer> validOrderCountList =  new ArrayList<>();

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Integer count = getOrdercount(beginTime,endTime,null);
            orderCountList.add(count);

            Integer validCount = getOrdercount(beginTime,endTime,Orders.COMPLETED);
            validOrderCountList.add(validCount);
        }

        //获取订单总数
        Integer totalorder = orderCountList.stream().reduce(Integer::sum).get();

        //获取有效订单数
        Integer valid = validOrderCountList.stream().reduce(Integer::sum).get();

        Double percent = 0.0;
        //获取订单完成率
        if (totalorder !=0){
            percent = valid.doubleValue() / totalorder;
        }

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalorder)
                .validOrderCount(valid)
                .orderCompletionRate(percent)
                .build();

    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> list = orderMapper.getTop10(beginTime,endTime);

        List<String> nameList = list.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String name = StringUtils.join(nameList, ",");

        List<Integer> numberList = list.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String number = StringUtils.join(numberList, ",");


        return SalesTop10ReportVO.builder()
                .nameList(name)
                .numberList(number)
                .build();
    }

    private Integer getOrdercount(LocalDateTime begin,LocalDateTime end,Integer status){
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);
        Integer count = orderMapper.getOrdercount(map);
        return count;
    }
}
